package com.autologistics.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.autologistics.model.Goods;
import com.autologistics.model.User;
import com.autologistics.service.UserInfoService;
import com.autologistics.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserInfoAction extends ActionSupport implements ModelDriven<User>{
	UserInfoService userInfoService;
	List<User> listuser;
	User user=new User();
	//去得到jsp页面中的新旧密码
	private String oldPassword;
	private String newPassword;
	private String msg;
	
	
	

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public List<User> getListuser() {
		return listuser;
	}

	public void setListuser(List<User> listuser) {
		this.listuser = listuser;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String list(){
		listuser=userInfoService.listAll();
		return "success";
	}
	
	public String preUpdatePersonInfo(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		user=userInfoService.getById(userid);
		return SUCCESS;
		
	}
	
	public String updatePersonInfo(){
		userInfoService.updateUser(user);
		return SUCCESS;
		
	}
	
	
	public String preCheckPersonInfo(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		user=userInfoService.getById(userid);
		return SUCCESS;
	}
	
	public String checkPersonInfo(){
		return SUCCESS;
	}

	public String preUpdatePassword(){
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		user=userInfoService.getById(userid);
		return SUCCESS;
	}
	
	public String updatePassword(){
		//首先得到当前的user
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpSession session=request.getSession();
		int userid=(int) session.getAttribute("id");
		user=userInfoService.getById(userid);
		//得到jsp页面的值
//		String oldPassword=(String)request.getAttribute("oldPassword");
//		String newPassword=(String)request.getAttribute("newPassword");
		
		System.out.println("JSP：oldPassword	"+oldPassword);
		System.out.println("newPassword	"+newPassword);
		

		//说明输入的密码是正确的
		if(user.getPwd().equalsIgnoreCase(oldPassword)){
			//更换密码
			System.out.println("原密码正确,成功更换密码");
			user.setPwd(newPassword);
			userInfoService.updateUser(user);
			//修改密码成功,跳转到主页上
			return SUCCESS;
		}else{
			msg="原密码错误";
			return ERROR;
		}
	}
	
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}
	
}
